$(function () {

    var currentDay = "今天";
    var index = "0";

    $(".investment_title2 a").each(function (i) {

        $(this).bind("click", function () {
            var url = $(this).attr("action");
            var data = "dayName=" + $(this).html();
            currentDay = data;
            index = i;
            yomAjaxPost(index, url, data);

        })
    });

    $(".pageList a").each(
        function () {
            $(this).bind("click", function () {
                var pageText = $(this).html();
                var pageIndex = $(this).attr("pageIndex");
                var pageCount = $(this).attr("pageCount");

                if (pageText == "上一页") {
                    pageIndex -= 1;
                    if (pageIndex < 1) {
                        pageIndex = 1;
                    }
                }
                else {
                    pageIndex += 1;
                    if (pageIndex >= pageCount) {
                        pageIndex = pageCount;
                    }
                }

                var url = $(this).attr("action");
                var data = "dayName=" + currentDay + "&pageNum=" + pageIndex;
                yomAjaxPost(index, url, data);
            });
        }
    );

    function yomAjaxPost(i, url, data) {
        $.ajax(
            {
                type: "post",
                url: url,
                data: data,
                dataType: "json",
                success: function (datas, textStatus) {
                    if (datas != null) {
                        if (datas.data != null && datas.data.length > 0) {
                            displayResult(i, JSON.parse(datas.data));
                        }
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {

                },
                complete: function (XMLHttpRequest, textStatus) {

                }
            }
        )
    }

    function displayResult(i, jsonArray) {
        $(".eventList").eq(i).empty();
        $(jsonArray).each(function () {
            $(".eventList").eq(i).append("<li><a href='#'>" + $this.newsTitle + "</a></li>");
        })
    }
});